#include "display.h"
#include "lcd_code.h"


extern uint8_t wifi_ctrl_block_time;
uint8_t led_seg_out[2]={0,0};


COMM_DIG_TX Com_Dig_Tx;


//extern uint8_t blink_flag_500ms;
//extern uint8_t blink_flag_1000ms;

void rinnai_delay_us(uint8_t cnt)
{
	uint8_t i=0,j=0;
	for(j=0;j<cnt;j++)
	{
		for(i=0;i<8;i++)NOP();

	}
}

uint8_t get_bit(uint16_t data,uint8_t bit_num)
{
    uint16_t dat;
    dat=(1<<bit_num);
    if(data & dat) {
       return 1;}
    else return 0;

}

void display_dig1(uint16_t data)
{
    Com_Dig_Tx.pin_info.pin1.dig_1A=get_bit(data,0);
    Com_Dig_Tx.pin_info.pin1.dig_1B=get_bit(data,1);
    Com_Dig_Tx.pin_info.pin1.dig_1C=get_bit(data,2);
    Com_Dig_Tx.pin_info.pin1.dig_1D=get_bit(data,3);   
    Com_Dig_Tx.pin_info.pin1.dig_1E=get_bit(data,4);
    Com_Dig_Tx.pin_info.pin1.dig_1F=get_bit(data,5);
    Com_Dig_Tx.pin_info.pin1.dig_1G=get_bit(data,6);

}

void display_dig2(uint16_t data)
{
	
    Com_Dig_Tx.pin_info.pin2.dig_2A=get_bit(data,0);
    Com_Dig_Tx.pin_info.pin2.dig_2B=get_bit(data,1);
    Com_Dig_Tx.pin_info.pin2.dig_2C=get_bit(data,2);
    Com_Dig_Tx.pin_info.pin2.dig_2D=get_bit(data,3);   
    Com_Dig_Tx.pin_info.pin2.dig_2E=get_bit(data,4);
    Com_Dig_Tx.pin_info.pin2.dig_2F=get_bit(data,5);
    Com_Dig_Tx.pin_info.pin2.dig_2G=get_bit(data,6);
}
uint8_t set_temp=0;
void display_run(uint8_t temp, uint8_t eco_code,uint8_t fire_code,uint8_t cricle,uint8_t pressure)
{
	display_dig1(led_number_code[(temp/10)%10]); 
	display_dig2(led_number_code[temp%10]);	
	if(eco_code==0x31){
		Com_Dig_Tx.pin_info.pin1.eco=1;
	}
	else if(eco_code==0x30){
		Com_Dig_Tx.pin_info.pin1.eco=0;
	}
	if(fire_code==0x01){
		Com_Dig_Tx.pin_info.pin2.fire=1;
	}
	else if(fire_code==0x00){
		Com_Dig_Tx.pin_info.pin2.fire=0;
	}
	if(cricle==0x31)
	{
		SEG9=1;
	}
	else if(cricle==0x30)
	{
		SEG9=0;
	}
	if(pressure==0x31)
	{
		SEG8=1;
	}
	else if(pressure==0x30)
	{
		SEG8=0;
	}
	
}
void display_version(uint8_t temp1, uint8_t temp2,uint8_t eco_code,uint8_t fire_code,uint8_t cricle,uint8_t pressure)
{
	display_dig1(led_number_code[temp1]); 
	display_dig2(led_number_code[temp2]);	
	if(eco_code==0x31){
		Com_Dig_Tx.pin_info.pin1.eco=1;
	}
	else if(eco_code==0x30){
		Com_Dig_Tx.pin_info.pin1.eco=0;
	}
	if(fire_code==0x01){
		Com_Dig_Tx.pin_info.pin2.fire=1;
	}
	else if(fire_code==0x00){
		Com_Dig_Tx.pin_info.pin2.fire=0;
	}
	if(cricle==0x31)
	{
		SEG9=1;
	}
	else if(cricle==0x30)
	{
		SEG9=0;
	}
	if(pressure==0x31)
	{
		SEG8=1;
	}
	else if(pressure==0x30)
	{
		SEG8=0;
	}
}
void display_err_fire(uint8_t err_code,uint8_t err_code_l,uint8_t err_eco_code,uint8_t err_fire_code,uint8_t cricle,uint8_t pressure)
{

	display_dig1(led_number_code[err_code]); 
	display_dig2(led_number_code[err_code_l]);
	//SEG8= 0; 
	if(err_eco_code==0x31){
		Com_Dig_Tx.pin_info.pin1.eco=1;
	}
	else if(err_eco_code==0x30){
		Com_Dig_Tx.pin_info.pin1.eco=0;
	}
	//SEG9= 0;
	if(err_fire_code==0x01){
		Com_Dig_Tx.pin_info.pin2.fire=1;
	}
	else if(err_fire_code==0x00){
		Com_Dig_Tx.pin_info.pin2.fire=0;
	}
	if(cricle==0x31)
	{
		SEG9=1;
	}
	else if(cricle==0x30)
	{
		SEG9=0;
	}
	if(pressure==0x31)
	{
		SEG8=1;
	}
	else if(pressure==0x30)
	{
		SEG8=0;
	}
}

void display_err_action(uint8_t ac_code,uint8_t ac_code_l,uint8_t ac_eco_code,uint8_t ac_fire_code,uint8_t ac_cricle,uint8_t ac_pressure)
{
	display_dig1(led_number_code[ac_code]); 
	display_dig2(led_number_code[ac_code_l]);	

	if(ac_eco_code==0x31){
		Com_Dig_Tx.pin_info.pin1.eco=1;
	}
	else if(ac_eco_code==0x30){
		Com_Dig_Tx.pin_info.pin1.eco=0;
	}

	if(ac_fire_code==0x01){
		Com_Dig_Tx.pin_info.pin2.fire=1;
	}
	else if(ac_fire_code==0x00){
		Com_Dig_Tx.pin_info.pin2.fire=0;
	}
	if(ac_cricle==0x31)
	{
		SEG9=1;
	}
	else if(ac_cricle==0x30)
	{
		SEG9=0;
	}
	if(ac_pressure==0x31)
	{
		SEG8=1;
	}
	else if(ac_pressure==0x30)
	{
		SEG8=0;
	}
}

void display_process(void)
{
	if (wifi_ctrl_block_time > 0)
	{
		wifi_ctrl_block_time--;
	}
	
	led_seg_out[0]=Com_Dig_Tx.pin_buf[0];
	led_seg_out[1]=Com_Dig_Tx.pin_buf[1];
	//SEG8= 1;//��
	//SEG9= 1;//zeng ya
	SEG10 = Com_Dig_Tx.pin_info.pin2.fire;
	SEG11 = Com_Dig_Tx.pin_info.pin1.eco;//
	//SEG12 = 0;//wifi
	//memcpy(led_seg_out,Com_Dig_Tx.pin_buf,sizeof(led_seg_out));
//	memset(Com_Dig_Tx.pin_buf,0, sizeof(Com_Dig_Tx.pin_buf));
}
   

void led_scan_dig(uint8_t dig)
{	
	switch (dig)
	{
		case 0://第一位亮
		    DISPLAY_DRIVER_DIG1 = 0;
			DISPLAY_DRIVER_DIG2 = 1;	    
		    break;
		case 1://全灭
			DISPLAY_DRIVER_DIG1 = 1;
			DISPLAY_DRIVER_DIG2 = 1;
			break;
		case 2://第二位亮
		    DISPLAY_DRIVER_DIG2 = 0;
			DISPLAY_DRIVER_DIG1 = 1;
		    break;
		case 3://全灭
			DISPLAY_DRIVER_DIG1 = 1;
			DISPLAY_DRIVER_DIG2 = 1;
			break;
		default:
		    break;
	}
//	if(dig==1)/*(dig<=4)*/
//		DISPLAY_DRIVER_DIG1=1;
//	else if(dig==4)/*(dig>4)*/
//		DISPLAY_DRIVER_DIG1=0;

//	if(dig==6)/*((dig>=6)&&(dig<10))*/
//		DISPLAY_DRIVER_DIG2=1;
//	else if(dig==10)/*((dig<6)||(dig>=10))*/
//		DISPLAY_DRIVER_DIG2=0;
}

void display_wifi(uint8_t icon)
{
	if(icon)SEG12=1;
	else SEG12=0;

}
void display_cricle(uint8_t cricle)
{
	if(cricle==0x31)
	{
		SEG9=1;
	}
	else
	{
		SEG9=0;
	}
}
void display_pressure(uint8_t pressure)
{
	
	if(pressure==0x31)
	{
		SEG8=1;
	}
	else
	{
		SEG8=0;
	}
}

void display_all(void)
{
	Com_Dig_Tx.pin_buf[0]=0xff;
	Com_Dig_Tx.pin_buf[1]=0xff;
	SEG8= 1;//��
	SEG9= 1;//zeng ya
	SEG12=1;//wifi
}
void display_clean(void)
{
	Com_Dig_Tx.pin_buf[0]=0x0;
	Com_Dig_Tx.pin_buf[1]=0x0;
	
	SEG8= 0;//��
	SEG9= 0;//zeng ya
	
	//SEG12=0;//wifi
}

void err_display_clean(uint8_t clean_eco_code,uint8_t clean_fire_code,uint8_t cricle,uint8_t pressure)
{
	if(clean_eco_code==0x31){
		Com_Dig_Tx.pin_buf[0]=0x80;
	}
	else if(clean_eco_code==0x30){
		Com_Dig_Tx.pin_buf[0]=0x0;
	}
	if(clean_fire_code==0x01){
		Com_Dig_Tx.pin_buf[1]=0x80;
	}
	else if(clean_fire_code==0x00){
		Com_Dig_Tx.pin_buf[1]=0x0;
	}
	if(cricle==0x31)
	{
		SEG9=1;
	}
	else if(cricle==0x30)
	{
		SEG9=0;
	}
	if(pressure==0x31)
	{
		SEG8=1;
	}
	else if(pressure==0x30)
	{
		SEG8=0;
	}
}

void DisplayDriverClear(void)
{
	DISPLAY_DRIVER_DIG1 = 1;
	DISPLAY_DRIVER_DIG2 = 1;
	SEG1= 0; 
	SEG2= 0; 
	SEG3= 0;
	SEG4= 0;
	SEG5= 0;
	SEG6= 0;   
	SEG7= 0;
	//SEG8= 0; 
	//SEG9= 0;
}

void DisplaySegClear(void)
{
	SEG1= 0; 
	SEG2= 0; 
	SEG3= 0;
	SEG4= 0;
	SEG5= 0;
	SEG6= 0;   
	SEG7= 0;
	//SEG8= 0; 
	//SEG9= 0;
}

void led_scan_seg(uint8_t segment,uint8_t segment_1)
{
  
	#if 0 
	SEG1= (!(segment&0x1)); 
	SEG2= (!(segment&0x2)); 
	SEG3= (!(segment&0x4));
	SEG4= (!(segment&0x8));
	SEG5= (!(segment&0x10));
	SEG6= (!(segment&0x20));   
	SEG7= (!(segment&0x40));
	SEG8= (!(segment&0x80));

	#else
	SEG1= (!(!(segment&0x1))); 
	SEG2= (!(!(segment&0x2))); 
	SEG3= (!(!(segment&0x4)));
	SEG4= (!(!(segment&0x8)));
	SEG5= (!(!(segment&0x10)));
	SEG6= (!(!(segment&0x20)));   
	SEG7= (!(!(segment&0x40)));
	

	#endif
    
 
}  